﻿using STMS.BLL;
using STMS.Models.DModels;
using STMS.WSTMSystem.FModels;
using STMS.WSTMSystem.UControls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.WSTMSystem
{
    public static class FormUtility
    {

        /// <summary>
        /// try  catch 异常处理
        /// </summary>
        /// <param name="act"></param>
        /// <param name="message"></param>
        public static void TryCatch(this Action act, string errTItle, string message)
        {
            try
            {
                act.Invoke();
            }
            catch (Exception ex)
            {
                MsgBoxHelper.MsgErrorShow(errTItle, ex.Message);
            }
        }

        /// <summary>
        /// 判断Form是否已打开
        /// </summary>
        /// <param name="formName"></param>
        /// <returns></returns>
        public static bool CheckOpenForm(string formName)
        {
            bool bResult = false;
            foreach (Form frm in Application.OpenForms)
            {
                if (frm.Name == formName)
                {
                    bResult = true;
                    break;
                }
            }
            return bResult;
        }

        /// <summary>
        /// 关闭指定的窗体
        /// </summary>
        /// <param name="formName"></param>
        public static void CloseOpenForm(string formName)
        {
            foreach (Form frm in Application.OpenForms)
            {
                if (frm.Name == formName)
                {
                    frm.Close();
                    break;
                }
            }
        }

        /// <summary>
        /// 返回指定的Form
        /// </summary>
        /// <param name="formName"></param>
        /// <returns></returns>
        public static Form GetOpenForm(string formName)
        {
            Form f = null;
            foreach (Form frm in Application.OpenForms)
            {
                if (frm.Name == formName)
                {
                    f = frm;
                    break;
                }
            }
            return f;
        }

        /// <summary>
        /// 添加窗体页面到选项卡中
        /// </summary>
        /// <param name="tab"></param>
        /// <param name="form"></param>
        public static void AddTabFormPage(this TabControl tab, Form form, int index=-1)
        {
            TabPage page = null;
            if (!CheckOpenForm(form.Name))
            {
                form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                form.TopLevel = false;
                form.WindowState = FormWindowState.Maximized;
                page = new TabPage();
                form.Parent = page;
                form.Dock = DockStyle.Fill;
                form.AutoSize = false;
                page.Name = form.Name;
                page.Text = form.Text;
                page.AutoScroll = true;
                if (index != -1)
                    tab.TabPages.Insert(index, page);
                else
                    tab.TabPages.Add(page);
                form.Show();
            }
            else
            {
                foreach (TabPage page0 in tab.TabPages)
                {
                    if (page0.Name == form.Name)
                    {
                        page = page0;
                        break;
                    }
                }
            }
            tab.SelectedTab = page;
         
        }



        /// <summary>
        /// 添加窗体页面到选项卡中
        /// </summary>
        /// <param name="tab"></param>
        /// <param name="form"></param>
        public static void AddPanelPage(this Panel panel, Form form)
        {
            if (CheckOpenForm(form.Name))
            {
                Form f = GetOpenForm(form.Name);
                bool bl = false;
                foreach(Control c in panel.Controls)
                {
                    if(c is Form)
                    {
                        Form cf = c as Form;
                        if(cf.Name==f.Name)
                        {
                            form = cf;
                            bl = true;
                            break;
                        }
                    }
                }
                if(!bl)
                {
                    form = f;
                }
            }
            panel.Controls.Clear();
            form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            form.TopLevel = false;
            form.WindowState = FormWindowState.Maximized;
            form.Dock = DockStyle.Fill;
            //form.AutoSize = false;
            panel.AutoScroll = true;
          
            panel.Controls.Add(form);
            form.Show();
            FrmMain fMain = panel.Parent as FrmMain;
            Panel panelList = null;
            foreach(Control c in fMain.Controls)
            {
                if(c is Panel && c.Name=="panelTopList")
                {
                    panelList = c as Panel;
                    SetTopBtnFontColor(panelList, form);
                    break;
                }
            }
        }

        public static void SetTopBtnFontColor(Panel panelTopList,Form frm)
        {
            foreach (Control upage in panelTopList.Controls)
            {
                if (upage is UPageButton)
                {
                    UPageButton btn = upage as UPageButton;
                    Color topSelColor = Color.FromArgb(45, 50, 116);
                    Color topUnSelColor = Color.White;
                    if (upage.Name == frm.Tag.ToString())
                    {
                        btn.SetBtnColor(topSelColor);
                    }
                    else
                    {
                        btn.SetBtnColor(topUnSelColor);
                    }
                }
            }
        }

        /// <summary>
        /// 清空所有分页选择
        /// </summary>
        /// <param name="panelTopList"></param>
        public static void SetAllTopBtnUnSelect(Panel panelTopList)
        {
            foreach (Control upage in panelTopList.Controls)
            {
                if (upage is UPageButton)
                {
                    UPageButton btn = upage as UPageButton;
                    Color topUnSelColor = Color.White;
                    btn.SetBtnColor(topUnSelColor);
                }
            }
        }


        /// <summary>
        /// 设置4个常用列的显示（修改、删除、恢复、移除）
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="showDel"></param>
        public static void ShowDgvBtnCols(this DataGridView dgv,bool showDel)
        {
            dgv.ShowDgvCol("colEdit", !showDel);
            dgv.ShowDgvCol("colDel", !showDel);
            dgv.ShowDgvCol("colRecover", showDel);
            dgv.ShowDgvCol("colRemove", showDel);
        }

        /// <summary>
        /// 设置指定名称的列的显示
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="colName"></param>
        /// <param name="blShow"></param>
        public static void ShowDgvCol(this DataGridView dgv,string colName,bool blShow)
        {
            dgv.Columns[colName].Visible = blShow;
        }

        /// <summary>
        /// 将指定按钮的背景色设置为指定的颜色
        /// </summary>
        /// <param name="btn"></param>
        /// <param name="color"></param>
        public static void SetBtnColor(this UserControl btn,Color color)
        {
            Type type = btn.GetType();
            if (type.BaseType==typeof(UserControl))
            {
                if(type==typeof(UMenuButton))
                {
                    ((UMenuButton)btn).BgColor = color;
                }    
                else
                {
                    ((UPageButton)btn).FontColor = color;
                }
            }
        }

        /// <summary>
        /// 绑定仓库列表下拉框
        /// </summary>
        /// <param name="cbo"></param>
         public static void LoadStores(this ComboBox cbo)
        {
            StoreBLL storeBLL = new StoreBLL();
            List<StoreInfo> stores= storeBLL.GetAllStores();
            stores.Insert(0, new StoreInfo()
            {
                StoreId = 0,
                StoreName = "----请选择----"
            });
            cbo.DisplayMember = "StoreName";
            cbo.ValueMember = "StoreId";
            cbo.DataSource = stores;
            cbo.SelectedIndex = 0;
        }

        /// <summary>
        /// 绑定温度状态下拉框
        /// </summary>
        /// <param name="cbo"></param>
        public static void LoadTemperStatus(this ComboBox cbo)
        {
            List<TemperStateModel> statusList = new List<TemperStateModel>()
            {
                new TemperStateModel(){TemperState =-1,TemperStateText="全部"},
                new TemperStateModel(){TemperState =0,TemperStateText="低温"},
                new TemperStateModel(){TemperState =1,TemperStateText="正常"},
                new TemperStateModel(){TemperState =2,TemperStateText="高温"}
            };
            cbo.DisplayMember = "TemperStateText";
            cbo.ValueMember = "TemperState";
            cbo.DataSource = statusList;
            cbo.SelectedIndex = 0;
        }
    }
}
